package com.dxl.lanju.service;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dxl.common.model.Page;
import com.dxl.common.model.RespData;
import com.dxl.common.utils.Utils;
import com.dxl.lanju.dao.AreaDao;
import com.dxl.service.common.BaseService;
import com.lanju.entity.Area;

@Service
@Transactional
public class AreaService extends BaseService {
	private static final Logger LOG = Logger.getLogger(AreaService.class);
	@Autowired
	private AreaDao areaDao;
	
	public RespData save(Area area) {
		Area srcArea;
		if (Utils.isNotBlank(area.getId())) {
			srcArea = this.get(Area.class, area.getId());
			srcArea.setName(area.getName());
			srcArea.setDescription(area.getDescription());
			srcArea.setState(area.getState());
		} else {
			if (this.areaDao.exist(area.getName())) {
				return RespData.instance(RespData.FAIL, "区域已经存在！");
			}
			srcArea = area;
		}
		super.save(srcArea);
		return RespData.instance(RespData.SUCCESS, srcArea.getId());
	}
	
	public RespData del(String[] ids) {
		if (Utils.isEmpty(ids)) {
			return RespData.instance(RespData.SUCCESS);
		} else {
			try {
				this.del(Area.class, ids);
				return RespData.instance(RespData.SUCCESS);
			} catch (Exception e) {
				LOG.error(e.getMessage(), e);
				return RespData.instance(RespData.FAIL);
			}
		}
	}
	
	public RespData state(String[] ids, Integer state) {
		if (Utils.isEmpty(ids)) {
			return RespData.instance(RespData.SUCCESS);
		} else {
			try {
				super.state(Area.class, ids, state);
				return RespData.instance(RespData.SUCCESS);
			} catch (Exception e) {
				LOG.error(e.getMessage(), e);
				return RespData.instance(RespData.FAIL);
			}
		}
	}
	
	public Page<Area> list(String keyword, Integer state, Integer page, Integer rows) {
		return areaDao.list(keyword, state, page, rows);
	}
}
